﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Data;
using CPPEI.Coolzon.Data.MainServiceData;
using CPPEI.Coolzon.Service.Contract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Server.ServiceImplement
{
    public class ReqUpdateClientStatusHandler : BaseHandler<ReqUpdateClientStatusData, RespUpdateClientStatusData>
    {
        public ReqUpdateClientStatusHandler()
            : base(RESPONSE_SCENARIO.CONNENT, RESPONSE_COMMAND.CONNECT.UPDATECLIENTSTATUS)
        {
        }

        protected override RespUpdateClientStatusData OnHandle(Data.MainServiceData.Request request, ReqUpdateClientStatusData requestData)
        {
            CacheData.ControlDomain domain = CacheData.ServerData.Instance.GetDomain(request.Header.DomainId);
            if (domain != null)
            {
                Common.Logger.DebugToTag("MainService", "域{0},用户{1}的Status修改为{2}",
                    domain.Id,
                    request.Header.UserIdentity,
                    requestData.Status);
                CacheData.Client client = domain.GetClient(request.Header.UserIdentity);
                if (client != null)
                {
                    //如果client状态normal，设置client在域中
                    if(requestData.Status == ClientStatus.Normal)
                    {
                        client.State = CacheData.ClientState.InDomain;
                    }
                    client.Status = requestData.Status;

                    RespUpdateClientStatusData responseData = new RespUpdateClientStatusData();
                    responseData.UserAccount = client.UserAccount;
                    responseData.UserIdentity = client.UserIdentity;
                    responseData.UserName = client.UserAccount;
                    responseData.Status = requestData.Status;

                    // 通知控制端
                    NotifyConsoleClient(request, domain, responseData);

                    return null;
                }
                else
                {
                    Common.Logger.ErrorToTag("MainService", "域{0},用户{1}的Status修改为{2},用户不存在",
                        domain.Id,
                        request.Header.UserIdentity,
                        requestData.Status);
                }
            }
            else
            {
                Common.Logger.ErrorToTag("MainService", "域{0},用户{1}的Status修改为{2},域不存在",
                    request.Header.DomainId,
                    request.Header.UserIdentity,
                    requestData.Status);
            }
            return null;
        }

        /// <summary>
        /// 通知控制端有终端的状态改变了
        /// </summary>
        /// <param name="request"></param>
        /// <param name="domain"></param>
        private void NotifyConsoleClient(Data.MainServiceData.Request request, 
            CPPEI.Coolzon.Server.CacheData.ControlDomain domain,
            RespUpdateClientStatusData responseData)
        {
            CacheData.Client consoleClient = domain.GetConsoleClient();
            if (consoleClient == null)
            {
                Common.Logger.WarningToTag("MainService", "no consoleClient in domain:{0}", domain.Id);
            }
            else
            {
                Response response = new Response();
                response.Header = new ResponseHeader();
                response.Data = SerializeUtility.SerializeToBinary(responseData);
                response.Header.Scenario = Service.Contract.RESPONSE_SCENARIO.CONNENT;
                response.Header.Command = Service.Contract.RESPONSE_COMMAND.CONNECT.UPDATECLIENTSTATUS;
                response.Header.SendTicks = DateTime.Now.Ticks;
                response.Header.DataLength = response.Data.Length;
                response.Header.RequestHeader = request.Header;
                try
                {
                    if(consoleClient.Callback!=null)
                    {
                        consoleClient.Callback.OnReceiveResponse(response);
                    }
                }
                catch (Exception ex)
                {
                    Common.Logger.ErrorToTag("MainService", "通知控制端{0},域{1}里有终端的状态改变失败:{2}",
                        consoleClient.UserIdentity,
                        domain.Id,
                        ex);
                    consoleClient.Callback = null;
                }
            }
        }
    }
}
